<?php
class cJsForm
        {
        var $properties;//arrays (name=>value, value) (mixed array)
        var $select_tag_properties; //$default, $arrays, $default_name="", $restore_default=false
	var $window_properties;

        function getProperties()
                {
		$name=$this->properties["id"];
                foreach($this->properties as $key=>$value)
			{
			if($key=='onclick' || $key=='onchange' || $key=='onblur' || $key=='onfocus')
				$property.=(!is_int($key))?"var_".$name.".".$key."="."$value; \n":"\"$value \";";
			else
				$property.=(!is_int($key))?"var_".$name.".".$key."="."\"$value\"; \n":"$value ";
			}
		unset($this->properties);
                return $property;
                }

        function startForm()
                {
                return "<form ".$this->getProperties()." >";
                }
	
	function generateLabel()
                {
		$js="var var_".$this->properties[id]." =  document.createElement('label');\n";
		$js.="words=document.createTextNode(eval(gRowId+1));\n";
		$js.="var_".$this->properties[id].".appendChild(words);\n";
                return "$js ";
                }
		
        function generateInputTag()
                {
		$js="var var_".$this->properties[id]." =  document.createElement('INPUT');\n";
		$js.="var_".$this->properties[id].".onfocus= function(){this.style.borderColor='blue';};\n";
		$js.="var_".$this->properties[id].".onblur= function(){this.style.borderColor='black';};\n";
                return "$js ".$this->getProperties();
                }

        function generateSelectTag()
                {
		$name=$this->properties["id"];

		$js="var var_".$name." =  document.createElement('SELECT');\n";
                $js.=$this->getProperties();
		$js.="var opt_".$name." =  document.createElement('OPTION');\n";
		if($this->select_tag_properties["default_name"]!='')
			$js.="var_$name.options[0]=new Option('{$this->select_tag_properties[default_name]}','');";
		else
			$js.="var_$name.options[0]=new Option('--Select--','');";

                if($this->select_tag_properties["arrays"])
                        {
                        for($i=0,$j=1;$i< count($this->select_tag_properties["arrays"]);$i++,$j++)
                                $names[$j]=array_values($this->select_tag_properties["arrays"][$i]);
			if($names)
                        foreach($names as $key=>$value)
                                {
                                $value_array[]=$value[0];
                                $name_array[]=($value[1])?$value[1]:$value[0];
                                }

                        for($i=0,$j=1;$i< count($name_array);$i++,$j++)
                                {
				if($this->select_tag_properties["default"]!='' && $this->select_tag_properties["default"]==$value_array[$i])
					{
					$index=$j;
					}
				elseif($this->select_tag_properties["default"]=='')
					{
					$index=0;
					}
				$js.="var_$name.options[$j]=new Option('$name_array[$i]','$value_array[$i]');";
				}
			$js.="var_$name.options[$index].selected=true;";
                        }
		else
			{
			$j=1;
			}
		if($this->window_properties["add_new"])
			{
			$js.="var_$name.options[$j]=new Option('# ADD NEW #','create_new');";
			$js.="var_".$name.".onchange= function(){if(this.value=='create_new'){".$this->window_properties["add_new"]."};};";
			unset($this->window_properties);
			}
		$js.="var_".$name.".onfocus= function(){this.style.borderColor='blue';};\n";
		$js.="var_".$name.".onblur= function(){this.style.borderColor='black';};\n";
		return $js;
                }

        function endForm()
                {
                return "</form>";
                }
        }
?>
